package samfeng.erp.product.specification.service;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import samfeng.erp.product.specification.controller.admin.vo.SpecificationCreateReqVO;
import samfeng.erp.product.specification.controller.admin.vo.SpecificationExportReqVO;
import samfeng.erp.product.specification.controller.admin.vo.SpecificationPageReqVO;
import samfeng.erp.product.specification.controller.admin.vo.SpecificationUpdateReqVO;
import samfeng.erp.product.specification.convert.SpecificationConvert;
import samfeng.erp.product.specification.dal.dataobject.SpecificationDO;
import samfeng.erp.product.specification.dal.mysql.SpecificationMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static samfeng.erp.product.specification.emus.ErrorCodeConstants.SPECIFICATION_NOT_EXISTS;

/**
 * 产品规格 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class SpecificationServiceImpl implements SpecificationService {

    @Resource
    private SpecificationMapper specificationMapper;

    @Override
    public Long createSpecification(SpecificationCreateReqVO createReqVO) {
        // 插入
        SpecificationDO specification = SpecificationConvert.INSTANCE.convert(createReqVO);
        specificationMapper.insert(specification);
        // 返回
        return specification.getId();
    }

    @Override
    public void updateSpecification(SpecificationUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateSpecificationExists(updateReqVO.getId());
        // 更新
        SpecificationDO updateObj = SpecificationConvert.INSTANCE.convert(updateReqVO);
        specificationMapper.updateById(updateObj);
    }

    @Override
    public void deleteSpecification(Long id) {
        // 校验存在
        this.validateSpecificationExists(id);
        // 删除
        specificationMapper.deleteById(id);
    }

    private void validateSpecificationExists(Long id) {
        if (specificationMapper.selectById(id) == null) {
            throw exception(SPECIFICATION_NOT_EXISTS);
        }
    }

    @Override
    public SpecificationDO getSpecification(Long id) {
        return specificationMapper.selectById(id);
    }

    @Override
    public List<SpecificationDO> getSpecificationList(Collection<Long> ids) {
        return specificationMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<SpecificationDO> getSpecificationPage(SpecificationPageReqVO pageReqVO) {
        return specificationMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SpecificationDO> getSpecificationList(SpecificationExportReqVO exportReqVO) {
        return specificationMapper.selectList(exportReqVO);
    }

}
